{% extends 'base_backend.html' %}
{% load static %}
{% block header_tail %}
    <link rel="stylesheet" href="{% static 'public/CodeMirror/lib/codemirror.css' %}">
    <link rel="stylesheet" href="{% static 'public/ComboSelect/css/combo.select.css' %}">
    <link rel="stylesheet" href="{% static 'public/CodeMirror/theme/lesser-dark.css' %}">
    <link rel="stylesheet" href="{% static 'public/CodeMirror/addon/dialog/dialog.css' %}">
    <link rel="stylesheet" href="{% static 'public/CodeMirror/addon/search/matchesonscrollbar.css' %}">
    <link rel="stylesheet" href="{% static 'plugins/select2/select2.css' %}">
    <link rel="stylesheet" href="{% static 'public/ComboSelect/css/combo.select.css' %}">
    <link rel="stylesheet" href="{% static 'zhongbao/config/css/main.css' %}">
    <link rel="stylesheet" href="{% static 'public/Codemirror/theme/3024-night.css' %}">
    <link rel="stylesheet" href="{% static 'public/Codemirror/theme/erlang-dark.css' %}">
    <script src="{% static 'public/CodeMirror/lib/codemirror.js' %}"></script>
    <script src="{% static 'public/Codemirror/mode/python/python.js' %}"></script>
    <script src="{% static 'plugins\jQuery\jquery-2.2.3.min.js' %}"></script>
    <script src="{% static 'plugins/vue1/vue.js' %}"></script>

    <style>
        .login-banner {
            width: 100%;
            background: url("{% static 'public/img/banner2.jpg' %}") center bottom/cover no-repeat;
        }
    </style>
{% endblock header_tail %}

{% block section_content %}
<div>
        <div class="box-header with-border">
            <h3 class="box-title">{{ model_verbose_name }}</h3>
        </div>
        <!-- form start -->
        <div class="box box-primary">
            <div class="box-body">
                <form id="configForm" role="form" action="" method="post"
                      enctype="multipart/form-data"><input type='hidden' name='csrfmiddlewaretoken' value='X4KvkYSWJC04LeLiNhFhvD1WAzJuSkyi' />

                <div>
                    <div style="float: left;" class="input-group select-config-div">
                        <label for="id" class="control-label">配置ID：</label>
                        <p id="configId" type="text" class="config-input">{{ config.id }}</p>
                    </div>
                    <div style="float: left;" class="input-group select-config-div">
                        <label for="id" class="control-label">任务ID：</label>
                        <p id="taskId" type="text" class="config-input">{{ config.task_id }}</p>
                    </div>
                    <div style="float: left;" class="input-group select-config-div">
                        <label for="id" class="control-label">项目ID：</label>
                        <p type="text" class="config-input">{{ config.project_id }}</p>
                    </div>
                    <div style="float: left;" class="input-group select-config-div">
                        <label for="id" class="control-label">网站名称：</label>
                        <p type="text" class="config-input">{{ config.site_name }}</p>
                    </div>
                    <div style="float: left;" class="input-group select-config-div">
                        <label for="id" class="control-label">网站URL：</label>
                        <p type="text" class="config-input">{{ config.site_url }}</p>
                    </div>
                    <div style="float: left;" class="input-group select-config-div">
                        <label for="id" class="control-label">网站类型：</label>
                        <p type="text" class="config-input">{{ config.site_type }}</p>
                    </div>
                    <div style="float: left;" class="input-group select-config-div">
                        <label for="id" class="control-label">域名：</label>
                        <p type="text" class="config-input">{{ config.domain }}</p>
                    </div>
                    <div style="float: left;" class="input-group select-config-div">
                        <label for="id" class="control-label">一级域名：</label>
                        <p type="text" class="config-input">{{ config.first_domain }}</p>
                    </div>
                    <div style="float: left;" class="input-group select-config-div">
                        <label for="id" class="control-label">分配时间：</label>
                        <p type="text" id="allocation_flag" class="config-input"></p>
                    </div>
                    <div style="float: left;" class="input-group select-config-div">
                        <label for="id" class="control-label">通过时间：</label>
                        <p id="finish_flag" type="text" class="config-input"></p>
                    </div>
                    <div style="float: left;" class="input-group select-config-div">
                        <label for="id" class="control-label">分配代码：</label>
                        <p type="text" class="config-input">{{ config.process_type }}</p>
                    </div>
                    <div style="float: left;" class="input-group select-config-div">
                        <label for="id" class="control-label">任务备注：</label>
                        <p type="text" class="config-input">{{ config.note }}</p>
                    </div>
                    <div style="float: left;" class="input-group select-config-div">
                        <label for="id" class="control-label">录入时间：</label>
                        <p type="text" id="ctime" class="config-input"></p>
                    </div>
                    <div style="float: left;" class="input-group select-config-div">
                        <label for="id" class="control-label">用户ID：</label>
                        <p type="text" class="config-input">{{ config.uid }}</p>
                    </div>
                    <div style="float: left;" class="input-group select-config-div">
                        <label for="id" class="control-label">网站语言：</label>
                        <p type="text" class="config-input">{{ config.lang }}</p>
                    </div>
                    <div style="float: left;" class="input-group select-config-div">
                        <label for="id" class="control-label">任务描述：</label>
                        <p type="text" class="config-input">{{ config.description }}</p>
                    </div>
                    <div style="float: left;" class="input-group select-config-div">
                        <label for="id" class="control-label">任务状态：</label>
                            {% if config.is_expired == 1 %}
                                <p type="text" id="status" class="config-input">已过期</p>
                            {% else %}
                                {% for filter in filters %}
                                    {% if filter.status == config.status %}
                                        <p type="text" id="status" class="config-input">{{ filter.name }}</p>
                                    {% endif %}
                                {% endfor %}
                            {% endif %}
                    </div>
                </div>
                <script>
                    var $alloc = $("#allocation_flag");
                    {% if config.allocation_flag == None %}
                        var alloc = null;
                    {% else %}
                        var alloc = {{ config.allocation_flag }};
                    {% endif %}
                    if (alloc){
                        var newDate = new Date();
                        newDate.setTime(alloc * 1000);
                        $alloc.text(newDate.toLocaleDateString())
                    }

                    var $fin = $("#finish_flag");
                    {% if config.finish_flag == None %}
                        var fin = null;
                    {% else %}
                        var fin = {{ config.finish_flag }};
                    {% endif %}
                    if (fin){
                        var newDate = new Date();
                        newDate.setTime(fin * 1000);
                        $fin.text(newDate.toLocaleDateString())
                    }

                    var $ct = $("#ctime");
                    {% if config.ctime == None %}
                        var ct = null;
                    {% else %}
                        var ct = {{ config.ctime }};
                    {% endif %}
                    if (ct){
                        var newDate = new Date();
                        newDate.setTime(ct * 1000);
                        $ct.text(newDate.toLocaleDateString())
                    }
                </script>
                <div style="clear:both">
                    <hr>
                    <div class="form-group">
                        <p style="margin-left: 10px; color: #00a7d0;" id="code-help">
                            <span  style="margin-right: 10px;">配置代码：</span>
                            <span id="message" style="margin-right: 10px; color:red;"></span>
                        </p>
                        <input name="cid" value="{{ config.id }}" style="display:none;">
                        <div id="file_show">
                            <textarea name="code" id="code" cols="100%" rows="30" onclick="showMessage()">{{ config.code }}</textarea>
                        </div>
                    </div>
                    {% if not config.is_expired %}
                        {% if config.status == 3 or config.status == 6 %}
                            {% if config.id %}
                                <p class="btn btn-primary" onclick="_saveCode()">保存修改</p>
                            {% else %}
                                <p class="btn btn-primary" onclick="saveCode()">保存代码</p>
                                <!--<p class="btn btn-primary" onclick="addConfig()">保存修改</p>-->
                            {% endif %}
                        {% endif %}
                    {% endif %}
                    <br>
                    <script>
                        var editor = CodeMirror.fromTextArea($("#code")[0], {
                            lineNumbers: true,
                            mode: "python",
                            //lineWrapping: true
                            theme: "3024-night",
                            readOnly: true
                        });
                        {% if config.is_expired == 1 %}
                            var isExpired = {{ config.is_expired }};
                        {% else %}
                            var isExpired = 0;
                        {% endif %}
                        {% if config.status %}
                            var status = {{ config.status }};
                        {% else %}
                            var status = null;
                        {% endif %}
                        console.log(isExpired, status);
                        if (isExpired == 0 && (status == 3 || status == 6)){
                            console.log(editor.options);
                            editor.options.readOnly = false
                        }else{
                            if (isExpired == 1) {
                                editor.on("focus", function(){
                                    var response = {result: false, message: "因任务已被回收，所以禁止编辑代码了！"};
                                    toPage(response)
                                })
                            }else{
                                if (status == 4) {
                                    editor.on("focus", function(){
                                        var response = {result: false, message: "因任务已提交审核，所以禁止编辑代码了！"};
                                        toPage(response)
                                    })
                                }else if (status == 5){
                                    editor.on("focus", function(){
                                        var response = {result: false, message: "因任务已通过审核，所以禁止编辑代码了！"};
                                        toPage(response)
                                    })
                                }
                            }
                        }


                    </script>
                </div>
                    <!--确认修改弹出框-->
                    <div class="modal fade" id="cancel-window" tabindex="-1" role="dialog"
                         aria-labelledby="myModalLabel" style="top:30%">
                        <div class="modal-dialog" role="document">
                            <div class="panel panel-danger">
                                <div class="panel-heading">
                                    <h3 class="panel-title">确认要修改配置吗？</h3>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" id="cancel_close" class="btn btn-default"
                                            data-dismiss="modal">取消
                                    </button>
                                    <button type="submit" id="cancel-submit" class="btn btn-default">
                                        确定
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>

                <!-- 频道编辑弹出窗口-->
                <form action="/config/channels/delete/53783/" method="post"><input type='hidden' name='csrfmiddlewaretoken' value='X4KvkYSWJC04LeLiNhFhvD1WAzJuSkyi' />
                    <input type="text" id="channel-change-all" name="channel-change-all" class="no-display"
                           autocomplete="off">
                    <input type="text" id="channel-delete-all" name="channel-delete-all" class="no-display"
                           autocomplete="off">
                    <div class="modal fade" id="config-channel-editor" tabindex="-1" role="dialog"
                         aria-labelledby="myModalLabel">
                        <div class="modal-dialog modal-lg" role="document" style="width: 80%">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"
                                            id="close-button0"><span
                                            aria-hidden="true">×</span></button>
                                    <h4 class="modal-title" id="myModalLabel">频道修改（必须点击下方-提交-按钮后才能保存修改）</h4>
                                </div>
                                <div class="modal-body" style="width: 100%">
                                    <label for="">修改已添加</label>
                                    <table class="table table-bordered table-striped"
                                           style="width: 100%; table-layout: fixed">
                                        <thead>
                                        <tr>
                                            <th style="width: 75%">频道url</th>
                                            <th style="width: 15%">名称</th>
                                            <th style="width: 10%">操作</th>
                                        </tr>
                                        </thead>
                                        <tbody id="channel-tbody">

                                            <tr class="channel-tr" id="231007">
                                                <td class="channel-url"><span>http://www.sxgp.gov.cn/html/zyh/swsjhd/index.html</span>
                                                    <input type="text" class="no-display"
                                                           value="http://www.sxgp.gov.cn/html/zyh/swsjhd/index.html" style="width: 100%"
                                                           name="channel-url-231007"></td>
                                                <td class="channel-name"><span>市委书记活动</span>
                                                    <input type="text" class="no-display"
                                                           value="市委书记活动"
                                                           name="channel-name-231007">
                                                </td>
                                                <td>
                                                    <span class="btn-link channels-change" style="cursor: pointer"
                                                          tag="1">修改</span>
                                                    |
                                                    <span class="btn-link channels-delete" style="cursor: pointer"
                                                          tag="1">删除</span>
                                                </td>

                                            </tr>

                                            <tr class="channel-tr" id="231006">
                                                <td class="channel-url"><span>http://www.sxgp.gov.cn/html/lydt/index.html</span>
                                                    <input type="text" class="no-display"
                                                           value="http://www.sxgp.gov.cn/html/lydt/index.html" style="width: 100%"
                                                           name="channel-url-231006"></td>
                                                <td class="channel-name"><span>旅游动态</span>
                                                    <input type="text" class="no-display"
                                                           value="旅游动态"
                                                           name="channel-name-231006">
                                                </td>
                                                <td>
                                                    <span class="btn-link channels-change" style="cursor: pointer"
                                                          tag="1">修改</span>
                                                    |
                                                    <span class="btn-link channels-delete" style="cursor: pointer"
                                                          tag="1">删除</span>
                                                </td>

                                            </tr>

                                            <tr class="channel-tr" id="231005">
                                                <td class="channel-url"><span>http://www.sxgp.gov.cn/html/gggs/index.html</span>
                                                    <input type="text" class="no-display"
                                                           value="http://www.sxgp.gov.cn/html/gggs/index.html" style="width: 100%"
                                                           name="channel-url-231005"></td>
                                                <td class="channel-name"><span>人事信息</span>
                                                    <input type="text" class="no-display"
                                                           value="人事信息"
                                                           name="channel-name-231005">
                                                </td>
                                                <td>
                                                    <span class="btn-link channels-change" style="cursor: pointer"
                                                          tag="1">修改</span>
                                                    |
                                                    <span class="btn-link channels-delete" style="cursor: pointer"
                                                          tag="1">删除</span>
                                                </td>

                                            </tr>

                                            <tr class="channel-tr" id="231004">
                                                <td class="channel-url"><span>http://www.sxgp.gov.cn/html/gggs/index.html</span>
                                                    <input type="text" class="no-display"
                                                           value="http://www.sxgp.gov.cn/html/gggs/index.html" style="width: 100%"
                                                           name="channel-url-231004"></td>
                                                <td class="channel-name"><span>公告公示</span>
                                                    <input type="text" class="no-display"
                                                           value="公告公示"
                                                           name="channel-name-231004">
                                                </td>
                                                <td>
                                                    <span class="btn-link channels-change" style="cursor: pointer"
                                                          tag="1">修改</span>
                                                    |
                                                    <span class="btn-link channels-delete" style="cursor: pointer"
                                                          tag="1">删除</span>
                                                </td>

                                            </tr>

                                            <tr class="channel-tr" id="231003">
                                                <td class="channel-url"><span>http://www.sxgp.gov.cn/html/jrgz/index.html</span>
                                                    <input type="text" class="no-display"
                                                           value="http://www.sxgp.gov.cn/html/jrgz/index.html" style="width: 100%"
                                                           name="channel-url-231003"></td>
                                                <td class="channel-name"><span>今日关注</span>
                                                    <input type="text" class="no-display"
                                                           value="今日关注"
                                                           name="channel-name-231003">
                                                </td>
                                                <td>
                                                    <span class="btn-link channels-change" style="cursor: pointer"
                                                          tag="1">修改</span>
                                                    |
                                                    <span class="btn-link channels-delete" style="cursor: pointer"
                                                          tag="1">删除</span>
                                                </td>

                                            </tr>

                                            <tr class="channel-tr" id="231002">
                                                <td class="channel-url"><span>http://www.sxgp.gov.cn/html/zf/index.html</span>
                                                    <input type="text" class="no-display"
                                                           value="http://www.sxgp.gov.cn/html/zf/index.html" style="width: 100%"
                                                           name="channel-url-231002"></td>
                                                <td class="channel-name"><span>政府</span>
                                                    <input type="text" class="no-display"
                                                           value="政府"
                                                           name="channel-name-231002">
                                                </td>
                                                <td>
                                                    <span class="btn-link channels-change" style="cursor: pointer"
                                                          tag="1">修改</span>
                                                    |
                                                    <span class="btn-link channels-delete" style="cursor: pointer"
                                                          tag="1">删除</span>
                                                </td>

                                            </tr>

                                            <tr class="channel-tr" id="231001">
                                                <td class="channel-url"><span>http://www.sxgp.gov.cn/html/rd/index.html</span>
                                                    <input type="text" class="no-display"
                                                           value="http://www.sxgp.gov.cn/html/rd/index.html" style="width: 100%"
                                                           name="channel-url-231001"></td>
                                                <td class="channel-name"><span>人大</span>
                                                    <input type="text" class="no-display"
                                                           value="人大"
                                                           name="channel-name-231001">
                                                </td>
                                                <td>
                                                    <span class="btn-link channels-change" style="cursor: pointer"
                                                          tag="1">修改</span>
                                                    |
                                                    <span class="btn-link channels-delete" style="cursor: pointer"
                                                          tag="1">删除</span>
                                                </td>

                                            </tr>

                                            <tr class="channel-tr" id="231000">
                                                <td class="channel-url"><span>http://www.sxgp.gov.cn/html/sw/index.html</span>
                                                    <input type="text" class="no-display"
                                                           value="http://www.sxgp.gov.cn/html/sw/index.html" style="width: 100%"
                                                           name="channel-url-231000"></td>
                                                <td class="channel-name"><span>市委</span>
                                                    <input type="text" class="no-display"
                                                           value="市委"
                                                           name="channel-name-231000">
                                                </td>
                                                <td>
                                                    <span class="btn-link channels-change" style="cursor: pointer"
                                                          tag="1">修改</span>
                                                    |
                                                    <span class="btn-link channels-delete" style="cursor: pointer"
                                                          tag="1">删除</span>
                                                </td>

                                            </tr>

                                            <tr class="channel-tr" id="230999">
                                                <td class="channel-url"><span>http://www.sxgp.gov.cn/html/jjdt/index.html</span>
                                                    <input type="text" class="no-display"
                                                           value="http://www.sxgp.gov.cn/html/jjdt/index.html" style="width: 100%"
                                                           name="channel-url-230999"></td>
                                                <td class="channel-name"><span>经济动态</span>
                                                    <input type="text" class="no-display"
                                                           value="经济动态"
                                                           name="channel-name-230999">
                                                </td>
                                                <td>
                                                    <span class="btn-link channels-change" style="cursor: pointer"
                                                          tag="1">修改</span>
                                                    |
                                                    <span class="btn-link channels-delete" style="cursor: pointer"
                                                          tag="1">删除</span>
                                                </td>

                                            </tr>

                                        </tbody>
                                    </table>
                                    <div class="form-group" id="channel-display">
                                        <label for="channel-config-content">增加频道-格式:频道URL#频道名称回车</label>
                                        <textarea name="channel-config-content" class="form-control"
                                                  id="channel-config-content"
                                                  cols="30" style="height: 200px;"></textarea>
                                    </div>
                                </div>
                                <div class="modal-footer">
                                    <button type="submit" id="btn_submit" class="btn btn-primary">
                                        提交
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>

            </div>
        </div>
    </div>
    <script>
        function saveCode(){
            {% if not config.id %}
                var cid = null;
            {% else %}
                var cid = {{ config.id }};
            {% endif %}
            var tid = {{ config.task_id }};
            var _code = $("#code").val();
            var code = editor.getValue();
            if (!code){
                response = {result:false, message:"代码不能为空！"};
                toPage(response);
                return false;
            }
            if (_code == code){
                response = {result:false, message:"您没有改动您的代码！"};
                toPage(response);
                return false;
            }
            $.ajaxSetup({
                data:{
                    csrfmiddlewaretoken:'{{ csrf_token }}',
                    cid: cid,
                    tid: tid,
                    code: code
                }
            });
            $.post(
                "{% url 'task:save_code' %}",
                function(response){
                    console.log(response);
                    var url = "{% url 'task:detail' %}" + "?id=" + tid + "&type=task";
                    toPage(response, -1, url)
                }
            )
        }
        function _saveCode(){
            {% if not config.id %}
                var cid = null;
            {% else %}
                var cid = {{ config.id }};
            {% endif %}
            var tid = {{ config.task_id }};
            var _code = $("#code").val();
            var code = editor.getValue();
            if (_code == code){
                response = {result:false, message:"您没有改动您的代码！"};
                toPage(response);
                return false;
            }
            if (!code){
                response = {result:false, message:"代码不能为空！"};
                toPage(response);
                return false;
            }
            $.ajaxSetup({
                data:{
                    csrfmiddlewaretoken:'{{ csrf_token }}',
                    cid: cid,
                    tid: tid,
                    code: code
                }
            });
            $.post(
                "{% url 'task:save_code' %}",
                function(response){
                    //if (response.result == true){
                    //    $("#code").val(code);
                    //    response.result = false
                    //}
                    toPage(response, -1)
                }
            )
        }
        function addConfig(){
            var tid = {{ config.task_id }};
            var code = $("#code").val();
            var configFormData = {};
            var $configForm = $("#configForm");
            var arr = $configForm.serializeArray();
            $.each(arr, function(){
                configFormData[this.name] = this.value;
            });
            configFormData.csrfmiddlewaretoken = "{{ csrf_token }}";
            configFormData.tid = tid;
            console.log(configFormData);
            return false;
            if (!code){
                response = {result:false, message:"代码不能为空！"};
                toPage(response);
                return false;
            }
            $.ajaxSetup({
                data:configFormData
            });
            $.post(
                "{% url 'task:add_config' %}",
                function(response){
                    console.log(response);
                    toPage(response, -1)
                }
            )
        }
        function toPage(response, page, url){
            var sec = 2000;
            var message = response.message;
            var result = response.result;
            var $message = $("#message");
            if (message){
                $message.text(message);
            }
            setTimeout(function(){
                if (url){
                        window.location.href = url;
                }else{
                    if (page == -1){
                        window.location.href = document.referrer;
                    }else{
                        if (result == true){
                            window.location.href = "";
                        }else{
                            $message.text("");
                        }
                    }
                }
            }, sec);
        }
    </script>
{% endblock section_content %}